package com.lkk.cg.util;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

import java.io.File;

/**
 * Created by lkk on 2014/12/21.
 */
public class FileLengthValidator extends FieldValidatorSupport {

    private long maxLength = 2 * 1024 * 1024;

    public long getMaxLength() {
        return maxLength;
    }

    public void setMaxLength(long maxLength) {
        this.maxLength = maxLength;
    }

    public void validate(Object object) throws ValidationException {
        Object val = getFieldValue(getFieldName(), object);
        if (val instanceof File) {
            File file = (File) val;
            if (file.length() > maxLength)
                addFieldError(getFieldName(), object);
        }
    }
}
